<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>生成一个 PKCS5 v2 PBKDF2 字符串</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.openssl-pbkdf2" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pbkdf2</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">openssl_pbkdf2</span> &mdash; <span class="dc-title">生成一个 PKCS5 v2 PBKDF2 字符串</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.openssl-pbkdf2-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pbkdf2</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$salt</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$key_length</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$iterations</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$digest_algorithm</code><span class="initializer"> = &quot;sha1&quot;</span></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pbkdf2()</strong></span> 计算 PBKDF2 (Password-Based Key Derivation Function 2),
   在PKCS5 v2中定义的一个密钥的推导函数.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pbkdf2-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<code class="parameter">password</code></dt>

    <dd>

     <p class="para">
      派生密钥所生成的密码。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">salt</code></dt>

    <dd>

     <p class="para">
      PBKDF2 推荐一个不少于64位(8字节)的密码盐值。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">key_length</code></dt>

    <dd>

     <p class="para">
      希望输出密钥的长度。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">iterations</code></dt>

    <dd>

     <p class="para">
      需要的迭代次数 <a href="https://pages.nist.gov/800-63-3/sp800-63b.html#sec5" class="link external">&raquo;&nbsp;NIST 
      建议至少10,000次</a>.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">digest_algorithm</code></dt>

    <dd>

     <p class="para">
      在<span class="function"><a href="openssl_get_md_methods.html" class="function">openssl_get_md_methods()</a></span>中可选的散列或摘要算法.默认是 SHA-1.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pbkdf2-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功，返回原始二进制字符串 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pbkdf2-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-938">
    <p><strong>Example #1 openssl_pbkdf2() 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'yOuR-pAs5w0rd-hERe'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$salt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">12</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$keyLength&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">40</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$iterations&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$generated_key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pbkdf2</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$salt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$keyLength</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$iterations</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$generated_key</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$generated_key</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>


   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-pbkdf2-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="hash_pbkdf2.html" class="function" rel="rdfs-seeAlso">hash_pbkdf2()</a> - 生成所提供密码的 PBKDF2 密钥导出</span></li>
    <li class="member"><span class="function"><a href="openssl_get_md_methods.html" class="function" rel="rdfs-seeAlso">openssl_get_md_methods()</a> - 获取可用的摘要算法</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>